<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Linux 命令- cat | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Linux 命令- cat</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cat命令的用途是连接文件或标准输入并打印。这个命令常用来显示文件内容，或者将几个文件连接起来显示，或者从标准输入读取内容并显示，它常与重定向符号配合使用。 </p>
<h2 id="1．命令格式："><a href="#1．命令格式：" class="headerlink" title="1．命令格式："></a>1．命令格式：</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cat [选项] [文件]</div></pre></td></tr></table></figure>
<h2 id="2．命令功能："><a href="#2．命令功能：" class="headerlink" title="2．命令功能："></a>2．命令功能：</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>cat主要有三大功能：</strong></p>
<ol>
<li>一次显示整个文件:cat filename</li>
<li>从键盘创建一个文件:cat &gt; filename 只能创建新文件,不能编辑已有文件.</li>
<li>将几个文件合并为一个文件:cat file1 file2 &gt; file</li>
</ol>
<h2 id="3．命令参数："><a href="#3．命令参数：" class="headerlink" title="3．命令参数："></a>3．命令参数：</h2><ul>
<li>-A –show-all 等价于 -vET</li>
<li>-b –number-nonblank 对非空输出行编号</li>
<li>-e  等价于 -vE</li>
<li>-E –show-ends 在每行结束处显示 $</li>
<li>-n –number 对输出的所有行编号,由1开始对所有输出的行数编号</li>
<li>-s –squeeze-blank 有连续两行以上的空白行，就代换为一行的空白行 </li>
<li>-t 与 -vT 等价</li>
<li>-T –show-tabs 将跳格字符显示为 ^I</li>
<li>-u (被忽略)</li>
<li>-v –show-nonprinting 使用 ^ 和 M- 引用，除了 LFD 和 TAB 之外</li>
</ul>
<h2 id="4．使用实例："><a href="#4．使用实例：" class="headerlink" title="4．使用实例："></a>4．使用实例：</h2><h3 id="实例1：把-log2012-log-的文件内容加上行号后输入-log2013-log-这个文件里"><a href="#实例1：把-log2012-log-的文件内容加上行号后输入-log2013-log-这个文件里" class="headerlink" title="实例1：把 log2012.log 的文件内容加上行号后输入 log2013.log 这个文件里"></a>实例1：把 log2012.log 的文件内容加上行号后输入 log2013.log 这个文件里</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cat -n log2012.log log2013.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cat log2012.log </span></div><div class="line">2012-01</div><div class="line">2012-02</div><div class="line"></div><div class="line"></div><div class="line">======</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cat log2013.log </span></div><div class="line">2013-01</div><div class="line">2013-02</div><div class="line"></div><div class="line"></div><div class="line">2013-03</div><div class="line">======</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cat -n log2012.log log2013.log </span></div><div class="line">1  2012-01</div><div class="line">2  2012-02</div><div class="line">3</div><div class="line">4</div><div class="line">5  ======</div><div class="line">6  2013-01</div><div class="line">7  2013-02</div><div class="line">8</div><div class="line">9</div><div class="line">10  2013-03</div><div class="line">11  ======</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例2：把-log2012-log-和-log2013-log-的文件内容加上行号（空白行不加）之后将内容附加到-log-log-里。"><a href="#实例2：把-log2012-log-和-log2013-log-的文件内容加上行号（空白行不加）之后将内容附加到-log-log-里。" class="headerlink" title="实例2：把 log2012.log 和 log2013.log 的文件内容加上行号（空白行不加）之后将内容附加到 log.log 里。"></a>实例2：把 log2012.log 和 log2013.log 的文件内容加上行号（空白行不加）之后将内容附加到 log.log 里。</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cat -b log2012.log log2013.log log.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cat -b log2012.log log2013.log log.log</span></div><div class="line">1  2012-01</div><div class="line">2  2012-02</div><div class="line">3  ======</div><div class="line">4  2013-01</div><div class="line">5  2013-02</div><div class="line">6  2013-03</div><div class="line">7  ======[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例3：把-log2012-log-的文件内容加上行号后输入-log-log-这个文件里"><a href="#实例3：把-log2012-log-的文件内容加上行号后输入-log-log-这个文件里" class="headerlink" title="实例3：把 log2012.log 的文件内容加上行号后输入 log.log 这个文件里"></a>实例3：把 log2012.log 的文件内容加上行号后输入 log.log 这个文件里</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cat log.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cat log.log </span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cat -n log2012.log &gt; log.log</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cat -n log.log </span></div><div class="line">1  2012-01</div><div class="line">2  2012-02</div><div class="line">3</div><div class="line">4</div><div class="line">5  ======</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例4：使用here-doc来生成文件"><a href="#实例4：使用here-doc来生成文件" class="headerlink" title="实例4：使用here doc来生成文件"></a>实例4：使用here doc来生成文件</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cat &gt;log.txt &lt;&lt;EOF</span></div><div class="line">&gt; Hello</div><div class="line">&gt; World</div><div class="line">&gt; Linux</div><div class="line">&gt; PWD=$(<span class="built_in">pwd</span>)</div><div class="line">&gt; EOF</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ls -l log.txt </span></div><div class="line">-rw-r--r-- 1 root root 37 10-28 17:07 log.txt</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cat log.txt </span></div><div class="line">Hello</div><div class="line">World</div><div class="line">Linux</div><div class="line">PWD=/opt/soft/<span class="built_in">test</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意粗体部分，here doc可以进行字符串替换。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>备注：</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>tac (反向列示)</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tac log.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># tac log.txt </span></div><div class="line">PWD=/opt/soft/<span class="built_in">test</span></div><div class="line">Linux</div><div class="line">World</div><div class="line">Hello</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;tac 是将 cat 反写过来，所以他的功能就跟 cat 相反， cat 是由第一行到最后一行连续显示在萤幕上，而 tac 则是由最后一行到第一行反向在萤幕上显示出来！</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/2. Linux 命令/10. Linux 命令- cat/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
